package org.jianghu.base;

import android.content.Context;
import android.util.Log;

import org.jianghu.base.model.MsMessage;
import org.json.JSONException;
import org.json.JSONObject;
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;


/**
 * Created by Gtexpress on 16/11/30.
 */

public abstract class BaseRequest {

    private Context context;

    // 请求服务器是否正在加载中
    private boolean isLoading = false;

    public void BaseRequest(Context context) {
        this.context = context;
    }

    /**
     * 开始构造参数请求服务器
     *
     * @param requestCode
     */
    public abstract void doRequest(int requestCode);


    /**
     * 子类请求网络的回调函数
     */
    public abstract void requestCallBack(boolean isSuccess, int requestCode,
                                         MsMessage msm, String failedReason);


    /**
     * 子类调用去请求服务器
     *
     * @param showProgressDialog
     * @param cancelDialog
     * @param params
     */
    public void requestServer( final int requestCode,
                              boolean showProgressDialog, boolean cancelDialog,
                              final RequestParams params) {
        requestServer( requestCode, showProgressDialog, cancelDialog,
                params, "");
    }


    /**
     * 子类调用去请求服务器
     *
     * @param showProgressDialog
     * @param cancelDialog
     * @param params
     * @param
     */
    public void requestServer( final int requestCode,
                              boolean showProgressDialog, boolean cancelDialog,
                              final RequestParams params, String text) {
        if (isLoading) {// 如果已经有请求在加载，返回
            return;
        } else {
            isLoading = false;
        }
        x.http().post(params, new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                Log.i("gt",result + "---result->");

                MsMessage msMessage = paserJsonToMsMessage(result);
                if (null != msMessage) {
                    requestCallBack(true, requestCode, msMessage, "");
                } else {
                    requestCallBack(false, requestCode, null, "");
                }
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {
                requestCallBack(false, requestCode, null, ex.toString());
            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });

    }

    public MsMessage paserJsonToMsMessage(String json) {
        MsMessage msMessage = new MsMessage();
        try {

            JSONObject jsonObj = new JSONObject(json);

            if (json.contains("data\":[")) {// json数组
                msMessage.setData(jsonObj.getJSONArray("data"));
            } else {// json对象
                if (!jsonObj.get("data").toString().equals("null")) {// 判断对象是否为空
                    msMessage.setData(jsonObj.get("data").toString());
                }
            }

            msMessage.setMessage(jsonObj.getString("message"));
            msMessage.setResult(jsonObj.getInt("result"));

            switch (msMessage.getResult()) {
                case 0:
                    break;
                case 1:
//                    T.showShort(this, msMessage.getMessage());
                    msMessage = null;
                    break;
                case -1:
//                    User icon_login_user = SharedPrefUtils.getUser(this);
//                    icon_login_user.setPassword("");
//                    SharedPrefUtils.setUser(this, icon_login_user);
//                    startActivity(new Intent(this, LoginActivity.class));
                    msMessage = null;
                    break;
            }

        } catch (JSONException e) {
            return null;
        }
        return msMessage;
    }


    /**
     * 检查空页面
     *
     * @param needParams
     *            拓展参数
     */
    public abstract void doEmptyCheck(Object... needParams);
}
